package com.inside.manage.controller;

import com.aspose.words.Document;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.data.PictureRenderData;
import com.deepoove.poi.policy.HackLoopTableRenderPolicy;
import com.microsoft.schemas.vml.CTShape;
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.xmlbeans.XmlObject;


import javax.xml.namespace.QName;
import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * aspose JDK 1.8 以上就可以
 *导出word  参考 https://blog.csdn.net/weixin_47252341/article/details/133819382
 */
public class word {

    public static void main(String[] args) throws Exception {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.put("examiner_name", "老六");
        dataMap.put("examiner_sex", "nande");
        dataMap.put("examiner_pin", "6666666666666");
        dataMap.put("examiner_phone", "1111111111122222222222");
        dataMap.put("examiner_photo", new PictureRenderData(100, 200, "C:\\Users\\kefei\\Pictures\\Dingtalk_20240219104053.jpg"));

        // 自己制作得模板
        String moban = "C:\\file\\card\\examiner\\card.doc";
        String newPDF = "C:\\Users\\kefei\\Desktop\\aaa.pdf";


        String temp = newPDF.replace(".pdf", ".docx");
        run(moban, temp, "", dataMap);
        w2p(temp, newPDF);

//        File tempFile = new File(temp);
//        tempFile.delete();
    }


    public static void run(String from, String to, String dataName, Map<String, Object> dataMap) throws Exception {
        HackLoopTableRenderPolicy policy = new HackLoopTableRenderPolicy();
        Configure config = Configure.newBuilder().bind(dataName, policy).build();
        XWPFTemplate template = XWPFTemplate.compile(from, config).render(dataMap);
        // 设置word水印
        setWaterMark(template);
        // 将word文件输出
        FileOutputStream out = new FileOutputStream(to);
        template.write(out);
        out.flush();
        out.close();
        template.close();
    }

    public static void setWaterMark(XWPFTemplate template) {
        XWPFDocument doc = template.getXWPFDocument().getXWPFDocument(); // 或者使用 new XWPFDocument() 创建一个全新的文档]
        //获取页面页脚
        XWPFHeaderFooterPolicy headerFooterPolicy = doc.getHeaderFooterPolicy();
        //如果没有页眉页脚则创建
        if (headerFooterPolicy == null) {
            headerFooterPolicy = doc.createHeaderFooterPolicy();
        }
        //设置水印显示的内容
        headerFooterPolicy.createWatermark("word 文档测试水印");
        XWPFHeader header = headerFooterPolicy.getHeader(XWPFHeaderFooterPolicy.DEFAULT);
        //创建文本段落
        XWPFParagraph paragraph = header.getParagraphArray(0);
        XmlObject[] xmlobjects = paragraph.getCTP().getRArray(0).getPictArray(0).selectChildren(new QName("urn:schemas-microsoft-com:vml", "shape"));
        if (xmlobjects.length > 0) {
            CTShape ctshape = (CTShape) xmlobjects[0];
            //设置水印的颜色
            ctshape.setFillcolor("#f6f6f6");
            ctshape.setStyle(ctshape.getStyle() + ";rotation:315");//设置水印的样式  获取默认样式和旋315度
//            ctshape.setStyle(getWaterMarkStyle(ctshape.getStyle(),100)  + ";rotation:315");//设置自定义水印的样式，setStyle()方法中需传入样式字符串
        }
    }

    /**
     * 修改水印样式高度的方法,如果不想改高度可以不用次方法
     *
     * @param styleStr 之前的水印样式
     * @param height   需要改成的高度
     * @return 返回新的修改好的水印样式
     */
    public static String getWaterMarkStyle(String styleStr, double height) {
        //把拿到的样式用";"切割，切割后保存到数组中
        Pattern p = Pattern.compile(";");
        String[] strs = p.split(styleStr);
        //遍历保存的数据，找到高度样式，将高度改为参数传入高度的
        for (String str : strs) {
            if (str.startsWith("height:")) {
                String heightStr = "height:" + height + "pt";
                styleStr = styleStr.replace(str, heightStr);
                break;
            }
        }
        return styleStr;//返回改好的样式
    }

    public static void w2p(String wordPath, String pdfPath) throws Exception {
        Document document = new Document(wordPath);
        File outputFile = new File(pdfPath);
        document.save(outputFile.getAbsolutePath(), com.aspose.words.SaveFormat.PDF);
    }

}
